var app = getApp(), fetchApi = require("../../../utils/httpPost.js");
import Toast from '../../../component/vant/toast/toast';
import Dialog from '../../../component/vant/dialog/dialog';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    imgurl: app.getImgUrl(),
    loading: false,
    netWork: false,
    isLoading: true,
    order_id:0,
    orderinfo:[],
    address: [],
    scrollTop: 0,
    scrollHeight: 0,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.order_id = options.order_id
    this.onHttpInit()
    this.setData({
      scrollHeight: app.globalData.windowHeight + 4,
    })
  },
  /**
     * 获取页面数据的 http请求
     */
  onHttpInit: function () {
    var that = this
    fetchApi.httpPost('/product/order/detail', {
      order_id: that.data.order_id,
    }, function (res) {
      console.log("订单详情数据", res)
      that.setData({ isLoading: false,   loading: true, netWork: true  })
      if (res.data.code == 0) {
        var order = res.data.data.order
        console.log('order', order)
        that.setData({
          orderinfo: order, address: res.data.data.address
        })
      }
    }, function (res) {
        that.setData({ hasData: true, isLoading: false, loading: true,  netWork: true })
    })
  },
  cancelOrder:function(e){
    var order_id = e.currentTarget.dataset.orderid
    var that = this;
    var message = '是否确认取消该订单？'
    Dialog.confirm({
      title: '取消订单',
      message
    }).then(() => {
      that.onHttpCancelOrder(order_id)
    })
    .catch(() => {
      Dialog.close();
    });
  },
  onHttpCancelOrder: function (order_id){
    console.log('order_id',order_id)
    var that = this
    fetchApi.httpPost('/product/order/cancelOrder', {
      order_id: order_id,
    }, function (res) {
      console.log("取消订单 返回数据", res)
      if (res.data.code == 0) {
        Toast.success('取消成功');
        that.data.pageIndex = 1;
        that.onHttpInit()
      } else {
        Toast.fail(res.data.msg);
      }
    },function(){})
  },
  payMoney:function(t){
    console.log('payMoney',t);
    var order_id = t.currentTarget.dataset.orderid;
    wx.navigateTo({
      url: '/pages/pay/paySubmit/index?order_id=' + order_id,
    })
  },
})